package com.yoda.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.youdianda.common.RC;
import com.yoda.youdianda.common.ThreadContext;
import com.yoda.youdianda.domain.YddArticle;
import com.yoda.youdianda.domain.YddUserData;
import com.yoda.youdianda.mapper.YddUserDataMapper;
import com.yoda.youdianda.mapper.YddUserMapper;
import com.yoda.youdianda.service.YddUserDataService;
import com.yoda.youdianda.service.YddUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class YddUserDataServiceImpl implements YddUserDataService {
    @Autowired
    private YddUserDataMapper yddUserDataMapper;

    @Override
    public Integer selectCount(LambdaQueryWrapper<YddUserData> collectLqw) {
        Integer integer = yddUserDataMapper.selectCount(collectLqw);
        return integer;
    }

    @Override
    public void addYddUserData(Integer articleId, Integer type) {
        String id = ThreadContext.getId(RC.YDD_ID);
        YddUserData yddUserData = new YddUserData();
        yddUserData.setUserId(Integer.parseInt(id));
        yddUserData.setArticleId(articleId);
        yddUserData.setType(type);
        yddUserDataMapper.insert(yddUserData);
    }

    @Override
    public void delYddUserData(Integer articleId, Integer type) {
        String id = ThreadContext.getId(RC.YDD_ID);
        Integer userId = Integer.parseInt(id);
        LambdaQueryWrapper<YddUserData> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YddUserData::getUserId, userId);
        lqw.eq(articleId != null, YddUserData::getArticleId, articleId);
        lqw.eq(type != null, YddUserData::getType, type);
        yddUserDataMapper.delete(lqw);
    }

    @Override
    public Page<YddUserData> yddUserDataPage(Integer type, Integer page, Integer limit) {
        Page<YddUserData> yddUserDataPage = new Page<>(page, limit);
        LambdaQueryWrapper<YddUserData> lqw = new LambdaQueryWrapper<>();
        lqw.eq(type != null,YddUserData::getType,type);
        yddUserDataPage = yddUserDataMapper.selectPage(yddUserDataPage,lqw);
        return yddUserDataPage;
    }

    @Override
    public List<YddUserData> getYddUserData(Integer type) {
        String userId = ThreadContext.getId(RC.YDD_ID);
        LambdaQueryWrapper<YddUserData> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YddUserData::getUserId,userId);
        lqw.eq(YddUserData::getType,type);
        return yddUserDataMapper.selectList(lqw);
    }
}
